﻿<?php    
   // $name='ad_'.date('Y-m-d_h-m-s')
    //;
      
    $date = getdate();
    $strdate = $date['year'].'-'.$date['mon'].'-'.$date['mday'].'_'.
                $date['hours'].'-'.$date['minutes'].'-'.$date['seconds'];
    $fmtdate = $date['mday'].'/'.$date['mon'].'/'.$date['year'].' '.
                $date['hours'].':'.$date['minutes'].':'.$date['seconds'];
                
    //so dau la mscb- time-sdphan-thuoc, thoi tiet, dvtinh
    $name='d'.$cb['User']['canbo_id'].'_'.$strdate;//date('Y-m-d_h-m-s');
  
 //   ini_set('memory_limit','512M');
    $worksheets = '';
    
    // Sheet đầu tiên lưu thông tin của cán bộ
    $rows = $xls->row($xls->hcell('UID').$xls->hcell('CBID').$xls->hcell('Họ tên').$xls->hcell('Thời điểm tải về'));
    $rows .= $xls->row($xls->cell($cb['User']['id']).$xls->cell($cb['User']['canbo_id']).$xls->cell($cb['Canbo']['HOTEN']).$xls->cell($fmtdate));
    $worksheets .= $xls->worksheet('ThongTin', $rows);    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('tendv'));
    foreach ( $dvtinhs as $result ):   
    $cells = '';
       $cells .= $xls->Cell($result['dvtinhs']['id']);
       $cells .= $xls->Cell($result['dvtinhs']['tendv']);
      
    endforeach; 
    $worksheets .= $xls->worksheet('dvtinhs',$rows);
    
    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('doituong_id').
                            $xls->hcell('sotay_id').$xls->hcell('thuoc_id').
                            $xls->hcell('nss_id').$xls->hcell('LIEULUONG').
                            $xls->hcell('DVT_T').$xls->hcell('songay').
                            $xls->hcell('date_sua').$xls->hcell('dvtinh_id'));
                            
    foreach ( $sdungthuocs as $result ):   
     $cells = '';
        $cells .= $xls->Cell($result['sdung_thuocs']['id']);
        $cells .= $xls->Cell($result['sdung_thuocs']['doituong_id']);
        $cells .= $xls->Cell($result['sdung_thuocs']['sotay_id']);
        $cells .= $xls->Cell($result['sdung_thuocs']['thuoc_id']);
        $cells .= $xls->Cell($result['sdung_thuocs']['nss_id']);
        $cells .= $xls->Cell($result['sdung_thuocs']['LIEULUONG']);
        $cells .= $xls->Cell($result['sdung_thuocs']['DVT_T']);
        $cells .= $xls->Cell($result['sdung_thuocs']['songay']);
        $cells .= $xls->Cell($result['sdung_thuocs']['date_sua']);
         $cells .= $xls->Cell($result['sdung_thuocs']['dvtinh_id']);   
        $rows .= $xls->row($cells);
     endforeach; 
     $worksheets .= $xls->worksheet('sdungthuocs',$rows);
  
// bang sd_phanbons
    
  	 	 	 	 	 	 	 
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('nss_id').
                            $xls->hcell('loaiphan_id').$xls->hcell('sotay_id').
                            $xls->hcell('LL_P').$xls->hcell('DVT_P').
                            $xls->hcell('GHICHU_P').$xls->hcell('songay').
                            $xls->hcell('date_sua').$xls->hcell('dvtinh_id')
                            );
    foreach ( $sdphanbons as $result ):   
     $cells = '';
        $cells .= $xls->Cell($result['sd_phanbons']['id']);
        $cells .= $xls->Cell($result['sd_phanbons']['nss_id']);
        $cells .= $xls->Cell($result['sd_phanbons']['loaiphan_id']);
        $cells .= $xls->Cell($result['sd_phanbons']['sotay_id']);
        $cells .= $xls->Cell($result['sd_phanbons']['LL_P']);
        $cells .= $xls->Cell($result['sd_phanbons']['DVT_P']);
        $cells .= $xls->Cell($result['sd_phanbons']['GHICHU_P']);
        $cells .= $xls->Cell($result['sd_phanbons']['songay']);
        $cells .= $xls->Cell($result['sd_phanbons']['date_sua']);
         $cells .= $xls->Cell($result['sd_phanbons']['dvtinh_id']);   
        $rows .= $xls->row($cells);
     endforeach; 
     $worksheets .= $xls->worksheet('sdphanbons',$rows);
    
     // bang thoitiets
    $rows = $xls->row($xls->hcell('id').$xls->hcell('thoitiet_id').
                            $xls->hcell('canbo_id').$xls->hcell('TUNGAY').
                            $xls->hcell('DENNGAY').$xls->hcell('HUONGGIO').
                            $xls->hcell('tinh_id').
                            $xls->hcell('date_sua'));
    foreach ( $thoitiets as $result ):   
     $cells = '';
        $cells .= $xls->Cell($result['thoitiets']['id']);
        $cells .= $xls->Cell($result['thoitiets']['thoitiet_id']);
        $cells .= $xls->Cell($result['thoitiets']['canbo_id']);
        $cells .= $xls->Cell($result['thoitiets']['TUNGAY']);
        $cells .= $xls->Cell($result['thoitiets']['DENNGAY']);
        $cells .= $xls->Cell($result['thoitiets']['HUONGGIO']);
        $cells .= $xls->Cell($result['thoitiets']['tinh_id']);
        $cells .= $xls->Cell($result['thoitiets']['date_sua']);
        $rows .= $xls->row($cells);
     endforeach; 
     $worksheets .= $xls->worksheet('thoitiets',$rows);
     
// bang ts_tts
    $rows = $xls->row($xls->hcell('id').$xls->hcell('thoitiet_id').
                            $xls->hcell('thongso_id').$xls->hcell('T_SO').
                            $xls->hcell('date_sua'));
    foreach ( $tstts as $result ):   
     $cells = '';
        $cells .= $xls->Cell($result['ts_tts']['id']);
        $cells .= $xls->Cell($result['ts_tts']['thoitiet_id']);
        $cells .= $xls->Cell($result['ts_tts']['thongso_id']);
        $cells .= $xls->Cell($result['ts_tts']['T_SO']);
        $cells .= $xls->Cell($result['ts_tts']['date_sua']);
        $rows .= $xls->row($cells);
     endforeach; 
     $worksheets .= $xls->worksheet('tstts',$rows);	 	         
    
     
    // If you print the output to the screen set the proper headers
    $xls->setHeader($name);
    
    //Trong truong hop khong muon ma hoa
    echo $xls->workbook($worksheets);
   
   //Trong truong hop muon ma hoa
      
 /*     
    
    $data = $xls->workbook($worksheets); 
    
    $cryptastic = new cryptastic;
    $encData = $cryptastic->encrypt($data, true);
    if($encData === false){
        $session->setFlash('Lỗi mã hóa dữ liệu');
        $this->redirect(array('controller'=>'bks','action'=>'index'));
    }else{
        echo $encData;
    }
   */ 
   
?> 